package cn.stylefeng.guns.modular.rm.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.modular.rm.RMTest;
import cn.stylefeng.guns.modular.rm.entity.RmGameScore;
import cn.stylefeng.guns.modular.rm.mapper.RmGameScoreMapper;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Author xiesm
 * @Date 2024/4/17 16:45
 **/
@Service
public class RmGameScoreService extends ServiceImpl<RmGameScoreMapper, RmGameScore> {
    @Autowired
    private RmGameScoreMapper rmGameScoreMapper;
    @Lazy
    @Autowired
    private RmGameService rmGameService;

    public List<String> getGameScoreIds() {
        List<RmGameScore> list = rmGameScoreMapper.selectList(null);
        return list.stream().map(RmGameScore::getFkRmGameId).collect(Collectors.toList());
    }

    public String getGameInfo(String cookie) {
        List<String> oldGameids = getGameScoreIds();
        Date beginDate = new Date();
        String format = DateUtil.format(DateUtil.offsetDay(beginDate, 1), "yyyy-MM-dd");
        DateTime endDate = DateUtil.parse(format + " 12:20:00");
        List<String> ids = rmGameService.getGameids(beginDate, endDate);

        for (int i = 0; i < ids.size(); i++) {
            List<RmGameScore> saveList = new ArrayList<>();
            if (oldGameids.contains(ids.get(i))) {
                continue;
            }
            String gameid = ids.get(i);
            String body = rmGameService.getGames(cookie, "1", gameid);
            Document document = Jsoup.parse(body);

            Elements contents = document.getElementsByClass("content-card");

            for (int j = 0; j < 1; j++) {
                Element element = contents.get(j);
                Elements rows = element.getElementsByClass("content_row");

                Elements types = element.getElementsByClass("marketType");
                String typeStr = types.get(0).text();
                String type = null;
                switch (typeStr) {
                    case "波胆":
                        type = "1";
                        break;
//                    case "半场波胆":
//                        type = "2";
//                        break;
//                    case "首入球时间":
//                        type = "3";
//                        break;
                }

                JSONObject chatShow = RMTest.getShowInfo(document.getElementsByClass("chatShowIcon").get(0));

                for (int k = 0; k < rows.size(); k++) {
                    Element row = rows.get(k);
                    String option = row.getElementsByClass("table_option").text();
                    String rate = row.getElementsByClass("table_rate").text();
                    rate = rate.replace("%", "");
                    if (StrUtil.isNotBlank(rate)) {
                        rate = new BigDecimal(rate).divide(new BigDecimal("100")).toString();
                    }

                    RmGameScore score = RmGameScore.builder()
                            .fkRmGameId(gameid)
                            .type(type)
                            .score(option)
                            .rate(rate)
                            .capital(rate)
                            .build();
                    saveList.add(score);
                }
            }
            this.saveBatch(saveList);
        }
        return null;
    }
}
